package com.geoboard.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.geoboard.ActivityInterface;
import com.geoboard.R;
import com.geoboard.entity.Board;

public class PostsContainer extends Fragment {
	
	/**** Members **************************************************/
	
	private String TAG = this.getClass().getSimpleName();
	private MapFrag mMap;
	private Board mBoard;
	private EditText mEditPostText;
	private Button mPostButton;
	private FragmentTabHost mTabHost;

	/**** Android Callback Methods *********************************/
	@Override
	public void onCreate(Bundle savedInstanceState){
		Log.d(TAG, "onCreate()");
		super.onCreate(savedInstanceState);
		mMap = ((ActivityInterface)getActivity()).getMapFrag();
		mBoard = (Board) getArguments().getParcelable("com.GeoBoard.entity.board");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){		
		Log.d(TAG, "onCreateView()");
		// Inflate the layout for this fragment
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.frag_posts_container, container, false);	
		
        mTabHost = (FragmentTabHost) root.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("map").setIndicator("", getResources().getDrawable(android.R.drawable.ic_dialog_map)),
        		PostsMapFrag.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("text").setIndicator("", getResources().getDrawable(android.R.drawable.ic_menu_sort_by_size)),
                PostsTextFrag.class, null);
        
		mEditPostText = (EditText) root.findViewById(R.id.edtxtMessage);
		mPostButton = (Button) root.findViewById(R.id.btnPost);
		mPostButton.setOnClickListener(createOnClickListener());

        return root;		
	}

    @Override
    public void onDestroyView() {
    	Log.d(TAG, "onDestroyView()");
        super.onDestroyView();
        mTabHost = null;
    }
	
	@Override
	public void onResume(){
		Log.d(TAG, "onResume()");
		super.onResume();		
		if (mBoard != null)
			mMap.getPosts(mBoard);
	}
	
	private OnClickListener createOnClickListener () {
		return new OnClickListener () {
			@Override
			public void onClick(View v) { 
				//only create the board if a name has been entered
				if (mEditPostText.getText().toString() != null && 
						!mEditPostText.getText().toString().equals("")) {
					
		            //hide the keyboard
		            ((InputMethodManager) 
		            		getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
		            		.hideSoftInputFromWindow(mEditPostText.getWindowToken(), 0);

//					//TODO some sort of waiting animation
			        Toast.makeText(getActivity().getApplicationContext(), 
			        		"Sending...", Toast.LENGTH_LONG).show();
			        //send post to server
			        mMap.createPost(mEditPostText.getText().toString(), mBoard.getId());
				}
			}
		};
	}
}
